#include "aed_mousecomp.h"


AED_MouseComp::AED_MouseComp()
{
    m_enabled =  false;
}

AED_MouseComp& AED_MouseComp::operator =(const AED_MouseComp &comp){
    m_conds = comp.m_conds;
    m_enabled = comp.m_enabled;
    return *this;
}

void AED_MouseComp::AddCondition(const QString &name, const QString &type, int omega, bool inv){
    Condition cond(name,type,omega,inv);

    m_conds.append(cond);

}

QList<AED_MouseComp::Condition>* AED_MouseComp::GetConditionList(){
    return &m_conds;
}

bool AED_MouseComp::IsEnabled(){
    return m_enabled;
}

void AED_MouseComp::SetEnabled(bool enabled){
    m_enabled = enabled;
}

void AED_MouseComp::RemoveCondition(int index){
    m_conds.removeOne(m_conds[index]);
}

void AED_MouseComp::Save(QTextStream &file){
    if(!m_enabled)
        return;

    file << "   <component = Mouse>" << endl;
    for(int i=0;i<m_conds.size();i++){
        file << "      <condition>" << endl;
        file << "         <name = "<< m_conds[i].name << ">" << endl;
        file << "         <type = "<< m_conds[i].type << ">" << endl;
        file << "         <omega = "<< m_conds[i].omega << ">" << endl;
        file << "         <inv = "<< m_conds[i].inv << ">" << endl;
        file << "      </condition>" << endl;
    }
    file << "   </component>" << endl;
}

void AED_MouseComp::Save(QDomDocument &doc, QDomElement &pelem){
    if(!m_enabled)
        return;
    QDomElement elem = doc.createElement("component");
    elem.setAttribute("comptype","Mouse");
    for(int i=0;i<m_conds.size();i++){
        QDomElement cond = doc.createElement("condition");
        cond.setAttribute("name", m_conds[i].name);
        cond.setAttribute("type", m_conds[i].type);
        cond.setAttribute("omega", m_conds[i].omega);
        cond.setAttribute("inv", m_conds[i].inv);
        elem.appendChild(cond);
    }
    pelem.appendChild(elem);
}

void AED_MouseComp::Load(QDomElement &celem){
    m_enabled = true;
    QDomNodeList conds = celem.elementsByTagName("condition");
    for(int i=0;i<conds.count();i++){
        QDomElement cond = conds.at(i).toElement();
        QString name = cond.attribute("name");
        int omega = cond.attribute("omega").toInt();
        bool inv = cond.attribute("inv").toInt();
        QString type = cond.attribute("type");
        AddCondition(name,type,omega,inv);
    }
}
